package cn.wetcland.es.service.impl;

import cn.wetcland.es.domain.EsGroupStudent;
import cn.wetcland.es.domain.EsStudent;
import cn.wetcland.es.dto.GroupAddDTO;
import cn.wetcland.es.dto.GroupBindStuDTO;
import cn.wetcland.es.dto.GroupDeleteDTO;
import cn.wetcland.es.dto.GroupRemoveStuDTO;
import cn.wetcland.es.mapper.EsClassMapper;
import cn.wetcland.es.mapper.EsGroupStudentMapper;
import cn.wetcland.es.mapper.EsStudentMapper;
import cn.wetcland.es.utils.CopyObjectUtil;
import cn.wetcland.es.utils.MyAssert;
import cn.wetcland.es.vo.GroupStuListItemVO;
import cn.wetcland.es.vo.GroupStuResItemVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wetcland.es.domain.EsGroup;
import cn.wetcland.es.service.EsGroupService;
import cn.wetcland.es.mapper.EsGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Wetcland
* @description 针对表【es_group(分组表)】的数据库操作Service实现
* @createDate 2023-04-29 17:04:49
*/
@Service
public class EsGroupServiceImpl extends ServiceImpl<EsGroupMapper, EsGroup> implements EsGroupService{

    @Resource
    private EsGroupMapper groupMapper;

    @Resource
    private EsClassMapper classMapper;

    @Resource
    private EsStudentMapper studentMapper;

    @Resource
    private EsGroupStudentMapper groupStudentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNewGroup(GroupAddDTO groupAddDTO) {
        MyAssert.notNull(classMapper.selectById(groupAddDTO.getClassId()), "该班级不存在");
        MyAssert.isNull(groupMapper.selectOne(new QueryWrapper<EsGroup>().eq("es_group_name", groupAddDTO.getGroupName())), "已存在同名组");
        EsGroup esGroup = new EsGroup();
        esGroup.setEsGroupClassId(groupAddDTO.getClassId());
        esGroup.setEsGroupName(groupAddDTO.getGroupName());
        esGroup.setEsGroupNum(groupAddDTO.getGroupNum());
        groupMapper.insert(esGroup);
    }

    @Override
    public List<GroupStuResItemVO> getGroupListByClassId(Long classId) {
        MyAssert.notNull(classMapper.selectById(classId), "该班级不存在");
        List<GroupStuResItemVO> resItemVOS = new ArrayList<>();
        List<GroupStuListItemVO> groupStuListItemVOS = groupMapper.selectGroupStuList(classId);
        Set<String> set = new HashSet<>();
        for (GroupStuListItemVO groupStuListItemVO : groupStuListItemVOS) {
            set.add(groupStuListItemVO.getEsGroupName());
        }
        for (String s : set) {
            List<GroupStuListItemVO> collect = groupStuListItemVOS.stream().filter(v -> v.getEsGroupName().equals(s)).collect(Collectors.toList());
            GroupStuResItemVO resItemVO = new GroupStuResItemVO();
            if (collect.size() > 0) {
                GroupStuListItemVO one = collect.get(0);
                resItemVO.setEsGroupId(one.getEsGroupId());
                resItemVO.setEsGroupName(one.getEsGroupName());
                resItemVO.setEsGroupNum(one.getEsGroupNum());
                resItemVO.setEsGroupClassId(one.getEsGroupClassId());
                resItemVO.setCreateTime(one.getCreateTime());
            }
            List<GroupStuResItemVO.Student> studentList = new ArrayList<>();
            for (GroupStuListItemVO groupStuListItemVO : collect) {
                GroupStuResItemVO.Student student = new GroupStuResItemVO.Student();
                student.setEsStudentId(groupStuListItemVO.getEsStudentId());
                student.setEsStudentSno(groupStuListItemVO.getEsStudentSno());
                student.setEsStudentName(groupStuListItemVO.getEsStudentName());
                student.setEsStudentGender(groupStuListItemVO.getEsStudentGender());
                studentList.add(student);
            }
            resItemVO.setStudents(studentList);
            resItemVOS.add(resItemVO);
        }
        return resItemVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGroup(GroupDeleteDTO groupDeleteDTO) {
        MyAssert.notNull(classMapper.selectById(groupDeleteDTO.getClassId()), "该班级不存在");
        MyAssert.notNull(groupMapper.selectById(groupDeleteDTO.getGroupId()), "该分组不存在");
        groupMapper.deleteById(groupDeleteDTO.getGroupId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindStuGroup(GroupBindStuDTO groupBindStuDTO) {
        System.out.println(groupBindStuDTO);
        MyAssert.notNull(classMapper.selectById(groupBindStuDTO.getClassId()), "该班级不存在");
        MyAssert.notNull(groupMapper.selectById(groupBindStuDTO.getGroupId()), "该分组不存在");
        List<EsStudent> esStudents = studentMapper.selectBatchIds(groupBindStuDTO.getStudentIds());
        MyAssert.isTrue(esStudents.size() == groupBindStuDTO.getStudentIds().size(), "绑定的学生ID存在重复或不存在");
        groupStudentMapper.delete(new QueryWrapper<EsGroupStudent>().eq("es_group_id", groupBindStuDTO.getGroupId()));
        for (Long studentId : groupBindStuDTO.getStudentIds()) {
            EsGroupStudent esGroupStudent = new EsGroupStudent();
            esGroupStudent.setEsGroupId(groupBindStuDTO.getGroupId());
            esGroupStudent.setEsStudentId(studentId);
            groupStudentMapper.insert(esGroupStudent);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeStu(GroupRemoveStuDTO groupRemoveStuDTO) {
        MyAssert.notNull(classMapper.selectById(groupRemoveStuDTO.getClassId()), "该班级不存在");
        MyAssert.notNull(groupMapper.selectById(groupRemoveStuDTO.getGroupId()), "该分组不存在");
        MyAssert.notNull(studentMapper.selectById(groupRemoveStuDTO.getStudentId()), "该学生不存在");
        EsGroupStudent esGroupStudent = groupStudentMapper.selectOne(new QueryWrapper<EsGroupStudent>()
                .eq("es_group_id", groupRemoveStuDTO.getGroupId())
                .eq("es_student_id", groupRemoveStuDTO.getStudentId())
        );
        MyAssert.notNull(esGroupStudent, "该绑定关系不存在");
        groupStudentMapper.deleteById(esGroupStudent.getEsGroupStudentId());
    }
}




